﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Domain.DAL;
using Utilities;
using Casanova.Web;

namespace WebApp.RepairingTickets
{
    public partial class RepairingTicketList : System.Web.UI.Page
    {
        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    BindRepeater();
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        #endregion

        #region Methods

        private DataSet GetDataSource(string repairingTicketHistoryIDs)
        {
            DataSet source = new DataSet();
            try
            {
                if (!string.IsNullOrEmpty(repairingTicketHistoryIDs))
                {
                    string ids = InitRepairingTicketHistoryIDs(repairingTicketHistoryIDs);
                    if (!string.IsNullOrEmpty(ids))
                    {
                        RepairingTicketDAO rtDAO = RepairingTicketDAO.GetInstance();
                        if (rtDAO != null)
                        {
                            source = rtDAO.GetForPrintTemplate(ids);
                        }
                    }
                }
            }
            catch (Exception)
            {
                source.Dispose();
                throw;
            }
            return source;
        }

        private string InitRepairingTicketHistoryIDs(string repairingTicketHistoryIDs)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(repairingTicketHistoryIDs))
            {
                string[] ids = repairingTicketHistoryIDs.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);
                if (ids != null && ids.Length > 0)
                {
                    foreach (string id in ids)
                    {
                        result = result + "'" + id + "',";
                    }

                    if (result.Trim().Length > 0)
                    {
                        result = result.Substring(0, result.Length - 1);
                    }
                }
            }
            return result;
        }

        private void BindRepeater()
        {
            try
            {
                string repairingTicketHistoryIDs = QueryStringUtility.GetString("IDs", string.Empty);
                if (!string.IsNullOrEmpty(repairingTicketHistoryIDs))
                {
                    DataSet ds = GetDataSource(repairingTicketHistoryIDs);

                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        rptResult.DataSource = ds;
                        rptResult.DataBind();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }
}